<?php
/**
 * 缓存工具
 *
 * @author        Louis.Liao <willjz@126.com>
 * @copyright     Copyright (c) 2007-2020 FoundCMS. All rights reserved.
 * @link          http://www.found5.com
 * @package       FoundCMS.Tools
 * @license       http://www.found5.com/license
 * @version       v3.1.0
 */

class XCache
{
    /**
     * 取缓存
     * @param $id
     * @return mixed|string
     */
    public static function get( $id ) {
        $value = Yii::app()->cache->get( $id );
        if ( $value === false )
            return '';
        else
            return $value;
    }

    /**
     * 设置缓存
     */
    public static function set( $id = '', $data = '', $expirse = 3600 ) {
        Yii::app()->cache->set( $id, $data, $expirse );
    }

    /**
     * 基础
     * @param $id
     * @param int $expirse
     * @param string $fields
     * @param array $params
     * @return array|mixed
     */
    public static function system( $id, $expirse = 600, $fields = '', $params = array() ) {
        $value = Yii::app()->cache->get( $id );
        if ( $value === false )
            return self::_refresh( $id, $expirse, $fields, $params );
        else
            return $value;
    }

    /**
     * 刷新缓存
     * @param $id
     * @param int $expirse
     * @param string $fields
     * @param array $params
     * @return array
     */
    public static function refresh( $id, $expirse = 600, $fields = '', $params = array() ) {
        return self::_refresh( $id, $expirse, $fields, $params );
    }

    /**
     * 刷新缓存
     * @param $id
     * @param $expirse
     * @param $fields
     * @param $params
     * @return array
     */
    private static function _refresh( $id, $expirse, $fields, $params ) {
        try {
            switch ( $id ) {
                case '_link':
                    $data = (array) self::_base( 'Link', $fields, array ( 'condition' => 'status=1' , 'order' => 'sort ASC,id DESC' ) );
                    self::set( $id, $data, $expirse );
                    break;
                case '_pca':
                    $data = (array) self::_base( 'Pca', $fields, array ( 'condition' => 'status=1' , 'order' => 'id ASC' ) );
                    self::set( $id, $data, $expirse );
                    break;
                case '_userGroup':
                    $data = (array) self::_base( 'UserGroup', $fields );
                    self::set( $id, $data, $expirse );
                    break;
                case '_ad':
                    $data = (array) self::_base( 'Ad', $fields, array ( 'condition' => 'status=1' , 'order' => 'sort ASC,id DESC' ) );
                    self::set( $id, $data, $expirse );
                    break;
                case '_options':
                    $data = (array) self::_options( $params );
                    self::set( $id, $data, $expirse );
                    break;
                case '_category':
                    $data = (array) self::_base( 'Category', $fields, array ( 'condition' => 'status=1' , 'order' => 'sort ASC,id ASC' ) );
                    self::set( $id, $data, $expirse );
                    break;
                default:
                    throw new Exception( '数据不在接受范围' );
                    break;
            }

            return $data;
        } catch ( Exception $error ) {
            exit( $error->getMessage() );
        }
    }

    /**
     * 基础数据
     * @param string $id
     * @param string $fields
     * @param string $condition
     * @return mixed
     */
    protected function _base( $id = '', $fields = '', $condition = '' ) {
        $mod = ucfirst( $id );
        $model = new $mod();
        $dataGet = $model->findAll( $condition );
        foreach ( (array) $dataGet as $key => $row ) {
            foreach ( (array) self::_attributes( $fields, $model ) as $attr )
                $returnData[$key][$attr] = $row->$attr;
        }
        return $returnData;
    }

    /**
     * 属性赋值
     * @param $data
     * @param $model
     * @param string $fields
     * @return mixed
     */
    protected function _attr2val( $data, $model, $fields = '' ) {
        foreach ( (array) $data as $key => $row ) {
            foreach ( (array) self::_attributes( $fields, $model ) as $attr )
                $returnData[$key][$attr] = $row->$attr;
        }
        return $returnData;
    }

    /**
     * 系统配置
     * @param string $params
     * @return mixed
     */
    protected function _options( $params = '' ) {
        $optionModel = Options::model()->findAll();
        foreach ( (array) $optionModel as $key => $row ) {
            if ( $params['type'] ) {
                if ( in_array( $row['type'], $params['type'] ) )
                    $returnData[$row['name']] = $row['value'];
            }else {
                $returnData[$row['name']] = $row['value'];
            }
        }
        return $returnData;
    }

    /**
     * 取字段
     * @param $fields
     * @param string $model
     * @return array
     */
    protected function _attributes( $fields, $model = '' ) {
        if ( empty( $fields ) || trim( $fields ) == '*' ) {
            return $model->attributeNames();
        } else {
            $fields = str_replace( '，', ',', $fields );
            return explode( ',', $fields );
        }
    }
}